# python data/clear_directories.py
import os
import shutil

# 定义需要清空的目录
DIRECTORIES_TO_CLEAR = ["features", "models", "predictions"]

def clear_directory(directory):
    """
    清空指定目录中的所有文件和子目录，但保留目录本身。
    """
    if os.path.exists(directory):
        for filename in os.listdir(directory):
            file_path = os.path.join(directory, filename)
            try:
                if os.path.isfile(file_path) or os.path.islink(file_path):
                    os.unlink(file_path)  # 删除文件或符号链接
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)  # 删除子目录
            except Exception as e:
                print(f"❌ 无法删除 {file_path}: {e}")
        print(f"✅ 已清空目录: {directory}")
    else:
        print(f"⚠️ 目录不存在，已跳过: {directory}")

if __name__ == "__main__":
    for directory in DIRECTORIES_TO_CLEAR:
        clear_directory(directory)